﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace HttpClient
{
	public enum HttpMethod
	{
		GET,
		POST,
		PUT,
		DELETE,
	}
	
	public class HttpRequest
	{
		private static String ComputeParameters(Dictionary<String, String> parameters)
        {
            var p = new List<string>();
            foreach (var parameter in parameters)
            {
                p.Add(parameter.Key + "=" + parameter.Value);
            }
            return String.Join("&", p.ToArray());
        }

        public String url;
        public Dictionary<String, String> urlParameters = new Dictionary<string, string>();
        public Dictionary<String, String> contentParameters = new Dictionary<string, string>();
		public Dictionary<String, String> headers = new Dictionary<string, string>();
		public HttpMethod method;
		public String password;
		public String userName;
		public byte[] content;
		public bool allowAutoRedirect = true;
		
		public String FullUrl {
			get {
				return url + (urlParameters.Count > 0 ? ("?" + ComputeParameters(urlParameters)) : "");
			}
		}

	    public byte[] Content
	    {
	        get {
                if (content != null)
                {
                    return content;
                }
                if (contentParameters.Count > 1)
                {
                    return Encoding.UTF8.GetBytes(ComputeParameters(contentParameters));
                }
	            return null;
	        }
	    }
	}
	
	public interface HttpResponse
	{
		HttpStatusCode StatusCode {get;}
		Stream Response {get;}
	}
	
    public interface HTTPClient
    {
        HttpResponse Execute(HttpRequest request);
    }
}
